/*
 * @Author: windys
 * @Description: 权限
 * @Date: 2021-12-06 09:24:11
 */
import router from '../router/index'
import store from '../store/index'
import type { RouteRecordRaw } from 'vue-router';
import { Storage } from '../utils/storage';

// 
store.dispatch('GET_ROUTES').then((res:any) => {
    res.forEach((item: any) => {
        router.addRoute(item)
    })
})
let hasRoles = true
router.beforeEach(async (to: any, from: any, next: any) => {
    if (to.path == '/login') {
        next()
    } else {
        // 用户信息
        if (Storage.get('user')) {
            // 拉取路由
            if (hasRoles) {
                const routes: any = await store.dispatch('GET_ROUTES')
                routes.forEach((item: any) => router.addRoute(item))
                hasRoles = false
                next({ ...to, replace: true }) // 这里相当于push到一个页面 不在进入路由拦截
            } else {
                next() // 如果不传参数就会重新执行路由拦截，重新进到这里
            }
        } else {
            next('/login')
        }
    }

})

